﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Touch2Like.DAL;
using System.Data;
using Touch2Like.Model;
using Touch2Like.SharedLibrary;
using System.Xml;


namespace Touch2Like.Web.Customer.Manage
{
    public partial class Question : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (WebUtility.UserGUID == null || WebUtility.UserGUID == "0")
            {
                Response.Clear();
                Response.Write("<script>parent.ResetURL('/Customer/signin.aspx?next=" + Server.UrlEncode("/Customer/Index.aspx") + "');</script>");
                Response.End();
            }

            if (!IsPostBack)
            {
                BindData();
            }
        }

        void BindData()
        {
            UserDAL user = new UserDAL();
            DataTable dtSetting = user.GetSettingByUserGUID(WebUtility.UserGUID);

            if (dtSetting.Select("FlagName='DisplayCoupon'").Length > 0)
            {
                if (dtSetting.Select("FlagName='DisplayCoupon'")[0]["Value"].ToString() == "true")
                    RadioButtonList_Choice.Items[0].Selected = true;
                else
                    RadioButtonList_Choice.Items[1].Selected = true;
            }

            if (dtSetting.Select("FlagName='URL'").Length > 0)
            {
                RadioButtonList_Choice.Items[2].Selected = true;
                TextBox_URL.Text = dtSetting.Select("FlagName='URL'")[0]["Value"].ToString();
            }

            string singleQuestion = "";
            string questionType = "";
            XmlDocument xml = new XmlDocument();
            QuestionDAL question = new QuestionDAL();
            DataTable dtSet = question.GetByUserGUID(WebUtility.UserGUID);
            DataTable dtAll = question.GetQuestionsByUserGUID(WebUtility.UserGUID);
            dtAll.Columns.Add("IsCheck");
            dtAll.Columns.Add("OrderNum");
            foreach (DataRow all in dtAll.Rows)
            {
                singleQuestion = all["Question"].ToString();

                if (singleQuestion.ToString().Contains("<Q>"))
                {
                    xml.LoadXml(singleQuestion);
                    all["Question"] = xml.SelectSingleNode("Q").FirstChild.InnerText;
                }
                all["Question"] = "&nbsp;" + all["Question"];

                questionType = all["QuestionType"].ToString();
                if (questionType == GlobalManager.QuestionType.YesNo)
                    all["QuestionType"] = " ( Yes/No )";
                else if (questionType == GlobalManager.QuestionType.MultiCheck)
                    all["QuestionType"] = " ( Multiple Choice )";
                else if (questionType == GlobalManager.QuestionType.SingleCheck)
                    all["QuestionType"] = " ( Single Selection )";
                else if (questionType == GlobalManager.QuestionType.Rate)
                    all["QuestionType"] = " ( Rating )";
                else if (questionType == GlobalManager.QuestionType.Comment)
                    all["QuestionType"] = " ( Comment )";
            }

            if (dtSet.Rows.Count > 0)
            {
                string setid = dtSet.Rows[0]["QuestionSetID"].ToString();
                DataTable dtQuestion = question.GetBySetID(setid);

                foreach (DataRow drAll in dtAll.Rows)
                {
                    drAll["IsCheck"] = "false";
                    drAll["OrderNum"] = "0";
                    if (dtQuestion.Select("ListID=" + drAll["ListID"].ToString()).Length > 0)
                    {
                        drAll["IsCheck"] = "true";
                        drAll["OrderNum"] = dtQuestion.Select("ListID=" + drAll["ListID"].ToString())[0]["OrderNum"].ToString();
                    }
                }
            }

            dtAll.DefaultView.RowFilter = "Void=0";
            dtAll = dtAll.DefaultView.ToTable();
            Repeater_Question.DataSource = dtAll;
            Repeater_Question.DataBind();
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            QuestionDAL question = new QuestionDAL();
            Guid setid = Guid.NewGuid();
            foreach (RepeaterItem item in Repeater_Question.Items)
            {
                DataRowView view = item.DataItem as DataRowView;
                CheckBox cb_Sel = item.FindControl("cb_Sel") as CheckBox;
                HiddenField tb_QuestionID = item.FindControl("tb_QuestionID") as HiddenField;
                HiddenField tb_OrderNum = item.FindControl("tb_OrderNum") as HiddenField;

                if (cb_Sel.Checked)
                {
                    QuestionSetInfo setInfo = new QuestionSetInfo();
                    setInfo.QuestionSetID = setid.ToString();
                    setInfo.UserGUID = WebUtility.UserGUID;
                    setInfo.QuestionID = Convert.ToInt32(tb_QuestionID.Value);
                    setInfo.OrderNum = 0;
                    if (tb_OrderNum.Value.Trim() != "")
                        setInfo.OrderNum = Convert.ToInt32(tb_OrderNum.Value);

                    question.AddQuestionSet(setInfo);

                    UserDAL user = new UserDAL();
                    UserSettingInfo settingInfo = new UserSettingInfo();
                    settingInfo.UserGUID = WebUtility.UserGUID;
                    settingInfo.FlagName = "DisplayCoupon";
                    settingInfo.Value = "";

                    if (RadioButtonList_Choice.SelectedItem != null)
                    {
                        if (RadioButtonList_Choice.SelectedItem.Value == "Coupon")
                        {
                            settingInfo.FlagName = "DisplayCoupon";
                            settingInfo.Value = "true";
                        }
                        else if (RadioButtonList_Choice.SelectedItem.Value == "EmailOnly")
                        {
                            settingInfo.FlagName = "DisplayCoupon";
                            settingInfo.Value = "false";
                        }
                        else if (RadioButtonList_Choice.SelectedItem.Value == "URL")
                        {
                            settingInfo.FlagName = "URL";
                            settingInfo.Value = TextBox_URL.Text.ToString();
                        }
                        user.UpdateSetting(settingInfo);
                    }
                }
            }
        }

        protected void Repeater_Question_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            RepeaterItem item = e.Item;
            DataRowView view = item.DataItem as DataRowView;

            CheckBox cb_Sel = item.FindControl("cb_Sel") as CheckBox;
            if (view["IsCheck"].ToString() == "true") cb_Sel.Checked = true;
            else cb_Sel.Checked = false;
        }
    }
}